﻿/**************************************************************************
创建时间:	2020/7/7 10:50:12    
作	  者:	张存
邮 	  箱:	zhangcunliang@126.com

Copyright (c) zhcun.cn

描	述：
记	录：
***************************************************************************/
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using ZhCun.Utils.Helpers;

namespace ZhCun.WebUtils.ControllersPage
{
    [ApiExplorerSettings(GroupName = "default", IgnoreApi = true)]
    [UserPageAuthorize(false)]
    public class StaticFileController : BasePageController
    {
        static Assembly _assembly = Assembly.GetAssembly(typeof(StaticFileController));

        [HttpGet]
        [Route("/{fileName}.js")]
        public IActionResult Test1(string fileName)
        {

            //assembly.GetManifestResourceStream()
            // = System.Reflection.Assembly.GetEntryAssembly();
            var sm = _assembly.GetManifestResourceStream($"ZhCun.WebUtils.wwwroot.{fileName}");
            if (sm == null)
            {
                return NotFound();
            }
            else
            {
                return File(sm, "application/javascript");
            }
        }

        string GetContentType(string fileName)
        {
            //image/png
            var extName = Path.GetExtension(fileName);
            string contentType;
            switch (extName.ToLower())
            {
                case ".png":
                case ".jpg":
                    contentType = "image/png";
                    break;
                case ".js":
                case ".json":
                    contentType = "application/javascript";
                    break;
                case ".css":
                    contentType = "text/css";
                    break;
                case ".txt":
                    contentType = "text/plain";
                    break;
                case ".woff2":
                case ".woff":
                    contentType = "font/woff2";
                    break;
                //
                default:
                    //".txt", "text/plain");
                    contentType = "application/octet-stream";
                    break;
            }
            return contentType;
        }

        [NonAction]
        IActionResult GetFileContent(string rootName, params string[] paths)
        {
            string fileName = $"ZhCun.WebUtils.wwwroot.{rootName}";

            foreach (var item in paths)
            {
                if (item.IsEmpty()) break;
                fileName += $".{item}";
            }
            var sm = _assembly.GetManifestResourceStream(fileName);
            if (sm == null)
            {
                return NotFound();
            }
            else
            {
                string contentType = GetContentType(fileName);
                return File(sm, contentType);
            }
        }

        [HttpGet]
        [ResponseCache(Duration = 10)]
        [Route("content/{path1}/{path2?}/{path3?}/{path4?}/{path5?}")]
        public IActionResult GetContent(string path1, string path2 = null, string path3 = null, string path4 = null, string path5 = null)
        {
            return GetFileContent("content", path1, path2, path3, path4, path5);
        }

        [HttpGet]
        [ResponseCache(Duration = 60, Location = ResponseCacheLocation.Client)]
        [Route("js/{path1}/{path2?}/{path3?}/{path4?}/{path5?}")]
        public IActionResult GetJS(string path1, string path2 = null, string path3 = null, string path4 = null, string path5 = null)
        {
            return GetFileContent("js", path1, path2, path3, path4, path5);
        }
    }
}